package com.jacobson.huobi.mvp.presenter

import com.jacobson.core.di.annotation.ActivityScope
import com.jacobson.core.mvp.BasePresenter
import com.jacobson.huobi.common.expansion.applySchedulersIoMain
import com.jacobson.huobi.common.expansion.onRespResult
import com.jacobson.huobi.common.expansion.onResponseResult
import com.jacobson.huobi.entities.events.LoginEvent
import com.jacobson.huobi.entities.evt.UpdPwdEvt
import com.jacobson.huobi.mvp.MainActivity
import com.jacobson.huobi.mvp.contract.EditPasswordContract
import com.jacobson.huobi.mvp.contract.KycAuthContract
import com.jacobson.huobi.mvp.fragment.SymbolFragment
import com.jacobson.huobi.threads.TokenThread
import com.jacobson.huobi.utils.TokenStorageUtil
import org.simple.eventbus.EventBus
import javax.inject.Inject

@ActivityScope
class EditPasswordPresenter @Inject constructor(
    view: EditPasswordContract.View,
    model: EditPasswordContract.Model
) : BasePresenter<EditPasswordContract.View, EditPasswordContract.Model>(view, model),
    EditPasswordContract.Presenter {

    override fun updatePwd(pwdEvt: UpdPwdEvt) {
        model.updatePwd(pwdEvt)
            .applySchedulersIoMain()
            .onResponseResult(view) {resp ->
                view?.onUpdatePwdResultResp(resp)
            }
    }

    override fun logout() {
        TokenThread.stop()
        TokenStorageUtil.save("")
        EventBus.getDefault().post(LoginEvent(1, null))

        model.logout().applySchedulersIoMain()
            .onResponseResult(view) {resp ->
                view?.onLogoutResultResp(resp)
            }
    }

    override fun getVertiCode(phone: String) {
        model.getVertiCode(phone)
            .applySchedulersIoMain()
            .onRespResult(view) { resp ->
                view?.onVertiCodeResultResp(resp)
            }
    }
}
